//
//  NewAddressTwoController.m
//  Youhuiju
//
//  Created by liuovyue on 16/8/12.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "NewAddressTwoController.h"
#import "ModelNewAddress.h"
#import "CityPicker.h"
#import "AddressCityViewController.h"
#import "ModelNewAddress.h"


@protocol cityDelegate <NSObject>

- (void)cityNSString:(NSString *)str;

@end


@interface NewAddressTwoController ()<UITableViewDelegate, UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UITextFieldDelegate,UIPickerViewDelegate ,UIPickerViewDataSource,addressDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIView *viewBar;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)NSIndexPath *indexPathNew;


@property (nonatomic, strong)NSString *strAddress;

@property (nonatomic, strong)UIButton *buttonSure;

@end

@implementation NewAddressTwoController




{
    NSInteger row1;
    NSInteger row2;
    NSInteger row3;
}


//- (void)dealloc {
//    
//    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"city" object:nil];
//    
//    
//}


//- (void)viewWillAppear:(BOOL)animated {
//    
//    self.arr = @[@"手机号码 :",@"所在地区 :"].mutableCopy;
//    self.view.backgroundColor = [UIColor whiteColor];
//    [self createNavigationBar];
//    [self createTableView];
//    
//    
//    
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    
    self.arr = @[@"手机号码 :",@"所在地区 :"].mutableCopy;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createNavigationBar];
    [self createTableView];
    
    
}

- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"新建收货地址";
    self.labelTitle.tintColor = [UIColor blackColor];
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
    
    self.buttonSure = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonSure.tintColor = [UIColor grayColor];
    [self.buttonSure setTitle:@"保存" forState: UIControlStateNormal];
    self.buttonSure.titleLabel.font = [UIFont systemFontOfSize:18*FitW];
    [self.buttonSure addTarget:self action:@selector(sureAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonSure];
    [self.buttonSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.viewBar.mas_right).offset(-17*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
    }];
    
    
    
    
}
- (void)tuihuiAcion:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)createTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64*FitH, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 127*FitH) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource= self;
    [self.tableView registerNib:[UINib nibWithNibName:@"CellNewAddressTwo" bundle:nil] forCellReuseIdentifier:@"CellNewAddressTwo"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CellHead" bundle:nil] forCellReuseIdentifier:@"CellHead"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CellAddressTwo" bundle:nil] forCellReuseIdentifier:@"CellAddressTwo"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CellArea" bundle:nil] forCellReuseIdentifier:@"CellArea"];
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.top.equalTo(self.viewBar.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        
        
    }];
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2 +self.arr.count;
}

#pragma mark -- tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.indexPathNew = indexPath;
    if (indexPath.row == 0) {
        self.cellOne = [tableView dequeueReusableCellWithIdentifier:@"CellHead"];
        self.cellOne.textFieldHead.returnKeyType = UIReturnKeyDone;
        
        self.cellOne.labelName.text = @"收货人 :";
        self.cellOne.textFieldHead.delegate = self;
        self.cellOne.selectionStyle = UITableViewCellSelectionStyleNone;
         self.cellOne.textFieldHead.text = self.labelNew.text;
        return self.cellOne;
    }
        
         else if (indexPath.row == 1) {
                 self.cellTwo = [tableView dequeueReusableCellWithIdentifier:@"CellNewAddressTwo"];
             
             
            self.cellTwo.textFieldPho.delegate = self;
           
            self.cellTwo.textFieldPho.returnKeyType = UIReturnKeyDone;
            self.cellTwo.textFieldPho.alpha = 1;
           
            self.cellTwo.selectionStyle = UITableViewCellSelectionStyleNone;
            self.cellTwo.labelAddress.alpha = 1;
             self.cellTwo.labelTitle.text = [self.arr objectAtIndex:indexPath.row- 1];
             self.cellTwo.textFieldPho.text = self.labelTel.text;
                 return self.cellTwo;
        }
  
    
        else if (indexPath.row == 2) {
            self.cellThree = [tableView dequeueReusableCellWithIdentifier:@"CellArea"];
            self.cellThree.labelAddress.text = self.labelQu.text;
            return self.cellThree;
            
        }
      else   {
            self.cellFour = [tableView dequeueReusableCellWithIdentifier:@"CellAddressTwo"];

            self.cellFour.textFieldAddressThree.placeholder = @"街道, 楼牌号等";
            self.cellFour.selectionStyle = UITableViewCellSelectionStyleNone;

          self.cellFour.textFieldAddressThree.text = self.labelDizhi.text;
          return self.cellFour;
        }
    
    
   
    
}
#pragma mark - 保存数据按钮
- (void)sureAcion:(UIButton *)button {
    
  
  
//    NSDictionary *dic = @{@"name":self.cellOne.textFieldHead.text,
//                          @"phone":self.cellTwo.textFieldPho.text,
//                          @"area":self.cellThree.labelAddress.text,
//                          @"address":self.cellFour.textFieldAddressThree.text};
    if (self.cellOne.textFieldHead.text != nil && self.cellTwo.textFieldPho.text != nil && self.cellThree.labelAddress.text!= nil && self.cellFour.textFieldAddressThree.text != nil) {
        
    
    NSDictionary *dic = @{@"name":self.cellOne.textFieldHead.text,
                          @"phone":self.cellTwo.textFieldPho.text,
                          @"area":self.cellThree.labelAddress.text,
                          @"address":self.cellFour.textFieldAddressThree.text};
    
       
       
    [[NSNotificationCenter defaultCenter]postNotificationName:@"address" object:nil userInfo:dic];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"shuaxin" object:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

//- (void)addPhotoAcion:(UIButton *)button {
//    
//    UIAlertController *alertcontroller  = [UIAlertController alertControllerWithTitle:@"修改头像" message:nil preferredStyle:UIAlertControllerStyleAlert];
//    
//    [alertcontroller addAction:[UIAlertAction actionWithTitle:@"拍照上传" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
//        
//        
//        
//        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//        
//        PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//      
//        PickerImage.allowsEditing = YES;
//      
//        PickerImage.delegate = self;
//      
//        [self presentViewController:PickerImage animated:YES completion:nil];
//        
//        
//        
//    }]];
//    [alertcontroller addAction:[UIAlertAction actionWithTitle:@"本地上传" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
//        
//       
//        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//       
//        PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
//        PickerImage.allowsEditing = YES;
//        PickerImage.delegate = self;
//        [self presentViewController:PickerImage animated:YES completion:nil];
//        
//        
//        
//    }]];
//    [self presentViewController:alertcontroller animated:YES completion:nil];
//    
//    
//}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return 101*FitH;
    }else {
    
    return 45*FitH;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
   
    
    ModelNewAddress *model = [[ModelNewAddress alloc]init];
    
    
    
    switch (indexPath.row) {
        case 1:{
            
            self.cellTwo.textFieldPho.userInteractionEnabled = YES;
            model.phone  = self.cellTwo.textFieldPho.text;
            
            
        }
            
            break;
            
            
        case 2:
        {
            
            
            
            
            [[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
            
            AddressCityViewController *city = [[AddressCityViewController alloc]init];
            city.delegate = self;
            [self.navigationController pushViewController:city animated:YES];
            
            
            
        }
            break;
        case 3:{
            


          
            
        }
            break;
        default:
            break;
    }
    
    
    
}

#pragma mark - 代理传入地址信息
- (void)passAddress:(UILabel *)labelAddress {
    
    self.cellThree.labelAddress.text = labelAddress.text;
   
   
}








- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    
    
    ModelNewAddress *model = [[ModelNewAddress alloc]init];
    //self.cellTwo.textFieldInformation.userInteractionEnabled = YES;
    //model.address  = self.cellTwo.textFieldInformation.text;
    //model.phone   =  self.cellTwo.textFieldInformation.text;
    if (textField.returnKeyType == UIReturnKeyNext) {
        model.name = self.cellOne.textFieldHead.text;
       
    }
    
    
    if (textField.returnKeyType == UIReturnKeyDone) {
       // [self.navigationController popViewControllerAnimated:YES];
        model.phone = self.cellTwo.textFieldPho.text;
      

       
        model.address  = self.cellTwo.textFieldInformation.text;
       
        
       // [[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(informationAction) name:@"dsf" object:nil];
        
    }
    
    
    
    
    

    
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
